<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/4
 * Time: 8:46
 */

namespace common\models\base;


use yii\db\ActiveRecord;

class Base extends ActiveRecord
{
    const PAGE_SIZE = 10;

    /**
     * @param $query 查询语句对象
     * @param $curPage 当前页数
     * @param int $pageSize 数据条数
     * @param null $search 查询语句
     * @return array
     */
    public function getPages($query,$curPage,$pageSize = 10,$search = null)
    {
        if($search){
            $query = $query->andFilerWhere($search);
        }

        $data['count'] = $query->count();
        if( !$data['count'] ){
            return ['count' => 0,'curPage'=>$curPage,'pageSize'=>$pageSize,'start' => 0,'end'=>0,'data'=>[]];
        }

        $curPage = ( ceil($data['count']/$pageSize)<$curPage )?ceil($data['count']/$pageSize):$curPage;


        $data['curPage'] = $curPage;
        $data['pageSize'] = $pageSize;
        $data['start'] = ($curPage-1)*$pageSize+1;
        $data['end'] = ceil($data['count']/$pageSize) == $curPage ? $data['count'] : ($curPage-1)*$pageSize+$pageSize ;

        $data['data'] = $query->orderBy( $this->lib_query->orderBy )->offset(($curPage-1)*$pageSize)->limit($pageSize)->asArray()->all();

        return $data;
    }

    public function getPages_V2($dataList,$count,$curPage = 1)
    {
        if($dataList){

        }

        $data['count'] = $query->count();
        if( !$data['count'] ){
            return ['count' => 0,'curPage'=>$curPage,'pageSize'=>$pageSize,'start' => 0,'end'=>0,'data'=>[]];
        }

        $curPage = ( ceil($data['count']/$pageSize)<$curPage )?ceil($data['count']/$pageSize):$curPage;

        $data['curPage'] = $curPage;
        $data['pageSize'] = $pageSize;
        $data['start'] = ($curPage-1)*$pageSize+1;
        $data['end'] = ceil($data['count']/$pageSize) == $curPage ? $data['count'] : ($curPage-1)*$pageSize+$pageSize ;

        $data['data'] = $query->offset(($curPage-1)*$pageSize)->limit($pageSize)->asArray()->all();

        return $data;
    }
}